<?php
/**
 * @version     1.0.0
 * @package     com_agents
 * @copyright   Copyright (C) 2014. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Nguyen Thanh Trung <nttrung211@yahoo.com> - http://
 */
// no direct access
defined('_JEXEC') or die;

JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
JHtml::_('behavior.keepalive');
// Import CSS
$document = JFactory::getDocument();
$document->addStyleSheet('components/com_agents/assets/css/agents.css');
?>
<script type="text/javascript">
    function getScript(url,success) {
        var script = document.createElement('script');
        script.src = url;
        var head = document.getElementsByTagName('head')[0],
        done = false;
        // Attach handlers for all browsers
        script.onload = script.onreadystatechange = function() {
            if (!done && (!this.readyState
                || this.readyState == 'loaded'
                || this.readyState == 'complete')) {
                done = true;
                success();
                script.onload = script.onreadystatechange = null;
                head.removeChild(script);
            }
        };
        head.appendChild(script);
    }
    getScript('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js',function() {
        js = jQuery.noConflict();
        js(document).ready(function(){
            
					js('input:hidden.city_id').each(function(){
						var name = js(this).attr('name');
						if(name.indexOf('city_idhidden')){
							js('#jform_city_id option[value="'+jQuery(this).val()+'"]').attr('selected',true);
						}
					});
					js('input:hidden.district_id').each(function(){
						var name = js(this).attr('name');
						if(name.indexOf('district_idhidden')){
							js('#jform_district_id option[value="'+jQuery(this).val()+'"]').attr('selected',true);
						}
					});

            Joomla.submitbutton = function(task)
            {
                if (task == 'daily.cancel') {
                    Joomla.submitform(task, document.getElementById('daily-form'));
                }
                else{
                    
                    if (task != 'daily.cancel' && document.formvalidator.isValid(document.id('daily-form'))) {
                        
                        Joomla.submitform(task, document.getElementById('daily-form'));
                    }
                    else {
                        alert('<?php echo $this->escape(JText::_('JGLOBAL_VALIDATION_FORM_FAILED')); ?>');
                    }
                }
            }
        });
    });
</script>
<style>
  #map_canvas {
    height: 300px;
  }
}
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script language="javascript">
function initialize() {
	var myLatlng = new google.maps.LatLng(<?php if($this->item->lat) echo $this->item->lat.','.$this->item->lng; else echo '10.997915,106.861410';?>);
	var mapOptions = {
	  zoom: 14,
	  center: myLatlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
	
	var marker = new google.maps.Marker({
		position: myLatlng,
		map: map
	});
	
	google.maps.event.addListener(map, "click", function(event) {
		var lat = event.latLng.lat();
		var lng = event.latLng.lng();
		// populate yor box/field with lat, lng
		document.getElementById('jform_lat').value = lat;
		document.getElementById('jform_lng').value = lng;
		
		marker.setMap(null);
		
		marker = new google.maps.Marker({
		  position: event.latLng,
		  map: map
		});
		
	});
}
window.addEvent('domready', function() {
	initialize();
});
</script>
<form action="<?php echo JRoute::_('index.php?option=com_agents&layout=edit&id=' . (int) $this->item->id); ?>" method="post" enctype="multipart/form-data" name="adminForm" id="daily-form" class="form-validate">
    <div class="width-60 fltlft">
        <fieldset class="adminform">
            <legend><?php echo JText::_('COM_AGENTS_LEGEND_DAILY'); ?></legend>
            <ul class="adminformlist">

                				<li><?php echo $this->form->getLabel('id'); ?>
				<?php echo $this->form->getInput('id'); ?></li>
				<li><?php echo $this->form->getLabel('name'); ?>
				<?php echo $this->form->getInput('name'); ?></li>
				<li><?php echo $this->form->getLabel('address'); ?>
				<?php echo $this->form->getInput('address'); ?></li>
				<li><?php echo $this->form->getLabel('phone'); ?>
				<?php echo $this->form->getInput('phone'); ?></li>
				<li><?php echo $this->form->getLabel('city_id'); ?>
				<?php echo $this->form->getInput('city_id'); ?></li>

			<?php
				foreach((array)$this->item->city_id as $value): 
					if(!is_array($value)):
						echo '<input type="hidden" class="city_id" name="jform[city_idhidden]['.$value.']" value="'.$value.'" />';
					endif;
				endforeach;
			?>				<li><?php echo $this->form->getLabel('district_id'); ?>
				<?php echo $this->form->getInput('district_id'); ?></li>

			<?php
				foreach((array)$this->item->district_id as $value): 
					if(!is_array($value)):
						echo '<input type="hidden" class="district_id" name="jform[district_idhidden]['.$value.']" value="'.$value.'" />';
					endif;
				endforeach;
			?>				<li><?php echo $this->form->getLabel('lat'); ?>
				<?php echo $this->form->getInput('lat'); ?></li>
				<li><?php echo $this->form->getLabel('lng'); ?>
				<?php echo $this->form->getInput('lng'); ?></li>
				<li><?php echo $this->form->getLabel('state'); ?>
				<?php echo $this->form->getInput('state'); ?></li>
				<li><div id="map_canvas"></div></li>

            </ul>
        </fieldset>
    </div>

    

    <input type="hidden" name="task" value="" />
    <?php echo JHtml::_('form.token'); ?>
    <div class="clr"></div>

    <style type="text/css">
        /* Temporary fix for drifting editor fields */
        .adminformlist li {
            clear: both;
        }
    </style>
</form>